import Vue from 'vue'
import Element from 'element-ui'
import vueiInfinite from 'vue-infinite-scroll'
import vueCropper from 'vue-cropper'
import App from './App'
import store from './store'
import router from './router'
import './icons' // icon
import './permission' // permission control
import './utils/error-log' // error log
import './styles/element-variables.scss'
import 'normalize.css/normalize.css' // a modern alternative to CSS resets
import '@/styles/index.scss' // global css
Vue.use(vueCropper)
Vue.use(vueiInfinite)
/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */
// import { mockXHR } from '../mock'
// if (process.env.NODE_ENV === 'production') {
//   mockXHR()
// }
import axios from 'axios'
Vue.use(Element, {
  size: 'small' // set element-ui default size
})
// register global utility filters
// Object.keys(filters).forEach(key => {
//   Vue.filter(key, filters[key])
// })
Vue.prototype.$beforeunload = false
Vue.config.productionTip = false
Vue.prototype.$getTableHeight = window.innerHeight - 208
Vue.prototype.$post = function(data) {
  return new Promise((resolve, reject) => {
    axios({
      url: data.url,
      method: 'post',
      data: data.data
    })
      .then(res => {
        if (res.data.code == 1000) {
          resolve(res.data.data)
        } else if (res.data.code === 2000) {
          this.$message.error('服务端请求失败，请联系管理员')
          reject()
        } else {
          this.$message.error(res.data.msg)
          reject()
        }
      })
      .catch(() => {
        this.$message.error('请刷新页面重试')
      })
  })
}
Vue.prototype.$monitorPost = (data) => {
  return new Promise((resolve, reject) => {
    axios({
      url: data.url,
      method: 'post',
      data: data.data,
      headers: {
        'Content-Type': 'application/json',
        Authorization: sessionStorage.token
            ? `Bearer ${sessionStorage.token}` : ''
      }
    })
      .then(res => {
        if (res.status == 200) {
          resolve(res.data.result)
        } else {
          reject('获取数据失败')
        }
      })
      .catch(() => reject('获取数据失败！'))
  })
}
Vue.prototype.$get = function(data) {
  return new Promise((resolve, reject) => {
    axios({
      url: data.url,
      method: 'get',
      params: data.data
    })
      .then(res => {
        if (res.data.code == 1000) {
          resolve(res.data.data)
        } else {
          this.$message.error(res.data.msg)
          reject(res.data.msg)
        }
      })
      .catch(err => {
        this.$message.error('请刷新页面重试')
        reject(err.response.data.error.message)
      })
  })
}
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
